package com.wocao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wocao.entity.ReqBody;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.ZyBuilding;
import com.wocao.entity.ZyUnit;
import com.wocao.mapper.ZyBuildingMapper;
import com.wocao.service.ZyUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 单元  前端控制器
 *
 * @since 2023-12-02
 */
@CrossOrigin
@RestController
@RequestMapping("/zyUnit")
public class ZyUnitController {
    @Autowired
    private ZyUnitService zyUnitService;

    @PostMapping("/selectUnitAll/{page}")
    public ResponseResult selectUnitAll(@PathVariable int page,@RequestBody ReqBody reqBody ){
        Page<ZyUnit> zyUnitPage = zyUnitService.selectUnitAll(new Page(page,reqBody.getSize()),reqBody);
        return ResponseResult.ok(zyUnitPage);
    }

    @PostMapping("unitAdd")
    public ResponseResult unitAdd(@RequestBody ZyUnit zyUnit){
        zyUnitService.unitAdd(zyUnit);
        return ResponseResult.ok("添加成功！");
    }

    @PutMapping("unitEdit")
    public ResponseResult unitEdit(@RequestBody ZyUnit zyUnit){
//        zyUnitService.update(zyUnit,new QueryWrapper<ZyUnit>().eq("unit_id",zyUnit.getUnitId()));
        zyUnitService.unitEdit(zyUnit);
        return ResponseResult.ok("修改成功！");
    }

    @DeleteMapping("/unitDelete/{unitId}")
    public ResponseResult unitDelete (@PathVariable long unitId){
        zyUnitService.removeById(unitId);
        return ResponseResult.ok("删除成功");
    }

    @DeleteMapping("/deleteUnits")
    public ResponseResult deleteUnits (@RequestParam("unitIds") String[] unitIds){
        zyUnitService.removeByIds(Arrays.asList(unitIds));
        return ResponseResult.ok("批量删除成功");
    }

    //导出
    @GetMapping("/derive")
    public void derive(HttpServletResponse response, @RequestParam("unitIds") String[] unitIds) throws IOException {
        zyUnitService.derive(response,unitIds);
    }

    @GetMapping("units/{buildingId}")
    public ResponseResult<ZyUnit> findUnitsByBuildingId(@PathVariable String buildingId) {
        List<ZyUnit> unitList = zyUnitService.getUnitsByBuildingId(buildingId);
        return ResponseResult.ok(unitList);
    }
}
